package com.bawei.wyg.mycommerce.splash.viewmodel;

import android.app.Application;
import android.content.Intent;

import androidx.annotation.NonNull;

import com.bawei.wyg.commonapplication.BaseApplication;
import com.bawei.wyg.mvvmcommon.mvvm.viewmodel.BaseViewModel;
import com.bawei.wyg.mycommerce.splash.data.TokenEntity;
import com.bawei.wyg.mycommerce.splash.model.SplashModel;
import com.bawei.wyg.mycommerce.splash.view.AllFragmentActivity;
import com.bawei.wyg.mycommerce.thread.TimerHandler;
import com.bawei.wyg.mycommerce.thread.TimerThread;
import com.bawei.wyg.networkmodel.entity.BaseEntity;
import com.bawei.wyg.networkmodel.utils.SpUtils;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class SplashViewModel extends BaseViewModel<SplashModel> implements TimerHandler.TimerCallBack {

    @Override
    protected void initData() {
        Map<String,Object> map=new HashMap<>();
        map.put("imie",""+System.currentTimeMillis());
        mModel.requestToken(map).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    @Override
    protected SplashModel createModel() {
        return new SplashModel();
    }

    @Override
    protected void sucessFull(BaseEntity baseEntity) {
        if(baseEntity instanceof TokenEntity){
            TokenEntity tokenEntity= (TokenEntity) baseEntity;
            SpUtils.saveData("token",tokenEntity.token);
            new TimerThread(new TimerHandler(this)).start();
        }
    }

    @Override
    public void callBack() {
        Intent intent = new Intent(BaseApplication.getAppContext(), AllFragmentActivity.class);
        BaseApplication.getAppContext().startActivity(intent);
//        Map<String,Object> map=new HashMap<>();
//        map.put("start","AllFragmentActivity.class");
//        UIChangeLiveData uiChangeLiveData = new UIChangeLiveData();
//        uiChangeLiveData.setStartActivity(map);
    }


}
